package net.xfok.switches;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.Settings;

public class ButtonScreenAlwaysOn extends TButton
{
	private final static String INTENT_ACTION = "net.xfok.switches.ButtonScreenAlwaysOn";
	private int screenTimeOut = -2;
	@Override
	public int canHandleIntent(Intent intent)
	{
		if(intent.getAction().equals(INTENT_ACTION))
			return ToggleWidget.HANDLING_TYPE_IMMEDIATE;
		else
			return ToggleWidget.HANDLING_TYPE_UNABLE;
	}

	@Override
	public int getButtonBmp(Context context)
	{
		if(screenTimeOut == -2)
			screenTimeOut = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 0);
		
		if(screenTimeOut == -1)
			return R.drawable.screen_on;
		else
			return R.drawable.screen_off;
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public void toggleSetting(Context context)
	{
		screenTimeOut = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 0);
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		screenTimeOut = (screenTimeOut == -1)?Integer.parseInt(sp.getString("Timeout", "15000")):-1;
		Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, screenTimeOut);
	}

}
